package com.example.primarycalculator2;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearSmoothScroller;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.ashokvarma.bottomnavigation.ShapeBadgeItem;
import com.ashokvarma.bottomnavigation.TextBadgeItem;

import java.util.ArrayList;

public class AnswerActivity extends AppCompatActivity implements BottomNavigationBar.OnTabSelectedListener{
    private AnswerAdapter adapter;
    ListView listView;


    public static final String TAG="AnswerActivity";


    BottomNavigationBar bottomNavigationBar;
    @Nullable
    TextBadgeItem numberBadgeItem;
    @Nullable
    ShapeBadgeItem shapeBadgeItem;
    int lastSelectedPosition = 0;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_answer);


        Intent intent=getIntent();
        int lefttime=intent.getIntExtra("lefttime",0);
        TextView textView=findViewById(R.id.time);
        int min=lefttime/60;
        int second=lefttime%60;
        String Min="";
        String Second="";
        if (second<10){
            Second="0"+second;
        }else {
            Second+=""+second;
        }
        if(min<10){
            Min="0"+min;
        }else {
            Min+=""+min;
        }
        textView.setText("剩余时间： "+Min+":"+Second);

        ArrayList<Problem> problems;
        problems = getIntent().<Problem>getParcelableArrayListExtra("list");
        ProblemsGenerator pg=new ProblemsGenerator();
        problems=pg.checkAnswer(problems);

//        计算做错了多少题
        SharedPreferences sharedPreferences=getSharedPreferences("menta", Activity.MODE_PRIVATE);
        int errnum=sharedPreferences.getInt("errnum",0);
        for(Problem problem:problems){
            if(problem.getPicId()!=R.drawable.right){
                errnum+=1;
            }
        }
        SharedPreferences.Editor editor= sharedPreferences.edit();
        editor.putInt("numerror",errnum);
        editor.apply();



        adapter=new AnswerAdapter(AnswerActivity.this,R.layout.answer_item, problems);
        listView=findViewById(R.id.answer_view);
        listView.setAdapter(adapter);




        bottomNavigationBar = findViewById(R.id.bottom_navigation_bar_answer);
        bottomNavigationBar.setTabSelectedListener(this);
        refresh();
    }

    @Override
    public void  onBackPressed(){}



    @SuppressLint("WrongConstant")
    private void refresh() {

        bottomNavigationBar.clearAll();
        numberBadgeItem = new TextBadgeItem()
                .setBorderWidth(4)
                .setBackgroundColorResource(R.color.blue)
                .setText("" + lastSelectedPosition)
                .setHideOnSelect(false);
        shapeBadgeItem = new ShapeBadgeItem()
                .setShape(5)
                .setShapeColorResource(R.color.teal)
                .setGravity(Gravity.TOP | Gravity.END)
                .setHideOnSelect(false);
        numberBadgeItem.toggle();
        shapeBadgeItem.toggle();
        bottomNavigationBar.setMode(2);
        bottomNavigationBar
                .addItem(new BottomNavigationItem(R.drawable.ic_home_white_24dp, "home").setActiveColorResource(R.color.orange).setBadgeItem(numberBadgeItem))
                .addItem(new BottomNavigationItem(R.drawable.ic_book_white_24dp, "error book").setActiveColorResource(R.color.teal))
                .addItem(new BottomNavigationItem(R.drawable.ic_github_circle_white_24dp, "user").setActiveColorResource(R.color.blue).setBadgeItem(shapeBadgeItem))
                .setFirstSelectedPosition(lastSelectedPosition > 2 ? 2 : lastSelectedPosition)
                .initialise();
    }






    @Override
    public void onTabSelected(int position) {
        lastSelectedPosition = position;
        Log.d(TAG, "onTabSelected: "+lastSelectedPosition);
        switch (lastSelectedPosition){
            case 0:
                Intent intent=new Intent(AnswerActivity.this,MainActivity.class);
                startActivity(intent);
                break;
            case 1:
                Intent intent1=new Intent(AnswerActivity.this,ErrorActivity.class);
                startActivity(intent1);
                break;
            case 2:
                Intent intent2=new Intent(AnswerActivity.this,UserActivity.class);
                startActivity(intent2);
                break;
        }

    }

    @Override
    public void onTabUnselected(int position) {

    }

    @Override
    public void onTabReselected(int position) {

    }
}